package com.slofzx.nuoda.evaluation.project.service.impl;

import com.slofzx.nuoda.common.dto.FileDTO;
import com.slofzx.nuoda.common.enums.ServiceResult;
import com.slofzx.nuoda.common.feign.FileInfoClient;
import com.slofzx.nuoda.common.service.FileInfoService;
import com.slofzx.nuoda.common.vo.FileVO;
import com.slofzx.nuoda.evaluation.project.controller.ReportInfoDTO;
import com.slofzx.nuoda.evaluation.project.dao.PlanInfoDAO;
import com.slofzx.nuoda.evaluation.project.dto.PlanInfoDTO;
import com.slofzx.nuoda.evaluation.project.dto.ReportInfoVO;
import com.slofzx.nuoda.evaluation.project.entity.PlanInfo;
import com.slofzx.nuoda.evaluation.project.enums.FileType;
import com.slofzx.nuoda.evaluation.project.service.ReportInfoService;
import com.slofzx.nuoda.evaluation.project.util.BusiUtil;
import com.slofzx.nuoda.evaluation.project.util.CglibUtils;
import org.apache.commons.collections4.CollectionUtils;
import org.apache.commons.collections4.MapUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.*;
import java.util.function.Function;
import java.util.stream.Collectors;

@Service
public class ReportInfoServiceImpl implements ReportInfoService {
    @Autowired
    private PlanInfoDAO planInfoDAO;
    @Autowired
    private FileInfoClient fileInfoClient;
    private static final String FILE_SOURCE_STR = "report";

    @Autowired
    private FileInfoService fileInfoService;

    @Override
    public List<ReportInfoVO> queryList(ReportInfoDTO dto) {
        Set<String> dataIdSet = null;
        Map<String, PlanInfo> planInfoMap = null;
        // 如果没有项目的筛选条件则获取项目，查询文件则不使用项目ID筛选
        if (StringUtils.isNotEmpty(dto.getBuildOffice()) || Objects.nonNull(dto.getYear()) ||
                StringUtils.isNotEmpty(dto.getPoolType()) || StringUtils.isNotEmpty(dto.getProjectName())) {
            // 查询项目
            final PlanInfoDTO planInfoDTO = new PlanInfoDTO();
            planInfoDTO.setBuildOffice(dto.getBuildOffice());
            planInfoDTO.setPoolType(dto.getPoolType());
            planInfoDTO.setYear(dto.getYear());
            planInfoDTO.setName(dto.getProjectName());
            final List<PlanInfo> planInfos = planInfoDAO.queryList(planInfoDTO);
            if (CollectionUtils.isEmpty(planInfos)) {
                return Collections.EMPTY_LIST;
            }
            planInfoMap = planInfos.stream()
                    .collect(Collectors.toMap(PlanInfo::getId, Function.identity()));

            dataIdSet = planInfoMap.keySet();
        }
        // 组装文件查询DTO
        final FileDTO fileDTO = new FileDTO();
        fileDTO.setTag(dto.getFileType());
        fileDTO.setDataIdSet(dataIdSet);
        fileDTO.setPageSize(dto.getPageSize());
        fileDTO.setPageNo(dto.getPageNo());
        fileDTO.setFileSource(FILE_SOURCE_STR);
        final List<FileVO> dataList = fileInfoService.findFileList(fileDTO);
        dto.setCount(dataList.size());

        if (CollectionUtils.isEmpty(dataList)) {
            return Collections.EMPTY_LIST;
        }
       // 如果开始没有获取项目列表则重新获取
        if (planInfoMap == null) {
            dataIdSet = dataList.stream().map(FileVO::getSourceId).collect(Collectors.toSet());
            final PlanInfoDTO planInfoDTO = new PlanInfoDTO();
            planInfoDTO.setIdList(dataIdSet);
            final List<PlanInfo> projectInfoVOS = planInfoDAO.queryList(planInfoDTO);
            if (CollectionUtils.isNotEmpty(projectInfoVOS)) {
                planInfoMap = projectInfoVOS.stream()
                        .collect(Collectors.toMap(PlanInfo::getId, Function.identity()));
            }
        }
        // 转换数据
        Map<String, PlanInfo> finalProjectInfoVOMap = planInfoMap;
        return dataList.stream().filter(Objects::nonNull).map(m -> {
            final ReportInfoVO reportInfoVO = new ReportInfoVO();
            CglibUtils.copy(m, reportInfoVO);
            // 根据项目map获取对应的项目名称
            if (MapUtils.isNotEmpty(finalProjectInfoVOMap)) {
                final PlanInfo planInfo = finalProjectInfoVOMap.get(reportInfoVO.getSourceId());
                if(Objects.nonNull(planInfo)) {
                    reportInfoVO.setProjectName(planInfo.getName());
                }
            }
            final FileType fileType = BusiUtil.getEnumById(reportInfoVO.getTag(), FileType.class);
            if (fileType != null) {
                reportInfoVO.setFileType(fileType.getName());
            }
            return reportInfoVO;
        }).collect(Collectors.toList());

    }

    @Override
    public List<ReportInfoVO> queryList2(ReportInfoDTO dto) {
//        Set<String> dataIdSet = null;
//        Map<String, ProjectInfoVO> projectInfoVOMap = null;
//        // 查询项目
//        final ProjectInfoDTO infoDTO = new ProjectInfoDTO();
//        infoDTO.setName(dto.get());
//        infoDTO.setPoolType(dto.getPoolType());
//        infoDTO.setBuildOffice(dto.getBuildOffice());
//        infoDTO.setYear(dto.getYear());
//        final List<ProjectInfoVO> projectInfoVOS = projectInfoService.queryListHasVersion(infoDTO);
//        if (CollectionUtils.isEmpty(projectInfoVOS)) {
//            return Collections.EMPTY_LIST;
//        }
//        projectInfoVOMap = projectInfoVOS.stream()
//                .collect(Collectors.toMap(ProjectInfoVO::getComputeId, Function.identity()));
//        dataIdSet = projectInfoVOMap.keySet();
//
//        // 组装文件查询DTO
//        final FileDTO fileDTO = new FileDTO();
//        fileDTO.setTag(dto.getFileType());
//        fileDTO.setDataIdSet(dataIdSet);
//        fileDTO.setPageSize(dto.getPageSize());
//        fileDTO.setPageNo(dto.getPageNo());
//        fileDTO.setFileSource(FILE_SOURCE_STR);
//        final List<FileVO> dataList = fileInfoService.findFileList(fileDTO);
//        dto.setCount(dataList.size());
//
//        if (CollectionUtils.isEmpty(dataList)) {
//            return Collections.EMPTY_LIST;
//        }
//
//        // 转换数据
//        Map<String, ProjectInfoVO> finalProjectInfoVOMap = projectInfoVOMap;
//        return dataList.stream().filter(Objects::nonNull).map(m -> {
//            final ReportInfoVO reportInfoVO = new ReportInfoVO();
//            CglibUtils.copy(m, reportInfoVO);
//            // 根据项目map获取对应的项目名称
//            if (MapUtils.isNotEmpty(finalProjectInfoVOMap)) {
//                final ProjectInfoVO projectInfoVO = finalProjectInfoVOMap.get(reportInfoVO.getSourceId());
//                if(Objects.nonNull(projectInfoVO)) {
//                    reportInfoVO.setProjectName(projectInfoVO.getName());
//                    reportInfoVO.setVersionId(projectInfoVO.getComputeId());
//                }
//            }
//            final FileType fileType = BusiUtil.getEnumById(reportInfoVO.getTag(), FileType.class);
//            if (fileType != null) {
//                reportInfoVO.setFileType(fileType.getName());
//            }
//            return reportInfoVO;
//        }).collect(Collectors.toList());
        return null;

    }

    @Override
    public ServiceResult save(FileDTO fileDTO) {
//        fileDTO.getDataIdSet()
        return null;
    }
}
